package com.lin.gulimall.gateway.config;//package com.lin.gulimall.gateway.config;
//
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.cors.CorsConfiguration;
//import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
//import org.springframework.web.cors.reactive.CorsConfigurationSource;
//import org.springframework.web.cors.reactive.CorsWebFilter;
//import org.springframework.web.filter.CorsFilter;
//import org.springframework.web.server.ServerWebExchange;
//
//@Configuration
//public class GulimallCorsConfiguration {
//
////
////    @Bean
////    public CorsWebFilter corsWebFilter(){
////        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
////        CorsConfiguration corsConfiguration = new CorsConfiguration();
////
////        //1、配置跨域
////        corsConfiguration.addAllowedHeader("*");
////        corsConfiguration.addAllowedMethod("*");
////        corsConfiguration.addAllowedOrigin("*");
////        corsConfiguration.addAllowCredentials(true);
////        corsConfiguration.validateAllowCredentials();
////
////        source.registerCorsConfiguration("/**",corsConfiguration);
////        return new CorsWebFilter((CorsConfigurationSource) source);
////    }
//
//    @Bean
//    public CorsFilter corsFilter() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        CorsConfiguration config = new CorsConfiguration();
//        config.setAllowCredentials(true);
//        // 允许访问的客户端域名
//        config.addAllowedOrigin("*");
//        // 允许服务端访问的客户端请求头
//        config.addAllowedHeader("*");
//        // 允许访问的方法名,GET POST等
//        config.addAllowedMethod("*");
//        // 对接口配置跨域设置
//        source.registerCorsConfiguration("/**" , config);
//        return new CorsFilter(source);
//    }
//
//
//}

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class GulimallCorsConfiguration implements WebMvcConfigurer {
    /**
     * 跨域配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //对那些请求路径进行跨域处理
        registry.addMapping("/**")
                // 允许的请求头，默认允许所有的请求头
                .allowedHeaders("*")
                // 允许的方法，默认允许GET、POST、HEAD
                .allowedMethods("*")
                // 探测请求有效时间，单位秒
                .maxAge(1800)
                // 支持的域
                .allowedOrigins("*");
    }
}
